package com.uuah.server.config.spring.parsers.specific;

import com.uuah.server.config.spring.parsers.delegate.ParentContextDefinitionParser;
import com.uuah.server.config.spring.parsers.generic.ChildDefinitionParser;
import com.uuah.server.config.spring.parsers.generic.UuahOrphanDefinitionParser;

/**
 * This allows a transformer to be defined globally, or embedded within an
 * endpoint (as either a normal or response transformer).
 */
public class TransformerDefinitionParser extends ParentContextDefinitionParser {

	public static final String TRANSFORMER = "transformer";
	public static final String RESPONSE_TRANSFORMER = "responseTransformer";
	public static final String RESPONSE_TRANSFORMERS = "response-transformers";

	public TransformerDefinitionParser(Class transformer) {
		super(UuahOrphanDefinitionParser.ROOT_ELEMENT,
				new UuahOrphanDefinitionParser(transformer, false));
		and(RESPONSE_TRANSFORMERS, new ChildDefinitionParser(
				RESPONSE_TRANSFORMER, transformer));
		otherwise(new ChildDefinitionParser(TRANSFORMER, transformer));
	}

	/**
	 * For custom transformers
	 */
	public TransformerDefinitionParser() {
		super(UuahOrphanDefinitionParser.ROOT_ELEMENT,
				new UuahOrphanDefinitionParser(false));
		and(RESPONSE_TRANSFORMERS, new ChildDefinitionParser(
				RESPONSE_TRANSFORMER));
		otherwise(new ChildDefinitionParser(TRANSFORMER));
	}

}
